﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Model;
using System.IO;
namespace LaKuaShop_TestDemo.Page
{
    /// <summary>
    /// OrderPage.xaml 的交互逻辑
    /// </summary>
    public partial class OrderPage : System.Windows.Controls.Page
    {
        double sum = 0;
        public BuyCartInfo BuyInfo;
        public OrderPage(BuyCartInfo buyCart)
        {
            InitializeComponent();
            BuyInfo = buyCart;
            IniOrderPage();
        }

        class OrderGood
        {
            public BitmapImage pic { get; set; }
            public string name { get; set; }
            public string num { get; set; }
            public string price { get; set; }
            public OrderGood(BitmapImage pic, string name, string num, string price)
            {
                this.pic = pic;
                this.name = name;
                this.num = num;
                this.price = price;
            }
        }
        void IniOrderPage()
        {
            Name.Text = string.Empty;
            Mobile.Text = string.Empty;
            Addr.Text = string.Empty;
            Name.Text = Common.User.name;
            Mobile.Text = Common.User.tel;
            Addr.Text = Common.User.address;
            List<OrderGood> order = new List<OrderGood>();
            BLL.Product Product = new BLL.Product();
            for (int i = 0; i < BuyInfo.PidList.Count; i++)
            {
                ProductInfo productInfo = Product.ShowProductInfo(BuyInfo.PidList[i]);
                byte[] img = Product.ShowProductImg(BuyInfo.PidList[i]);
                BitmapImage bmp = new BitmapImage();
                if (img == null)
                {
                    bmp = new BitmapImage(new Uri("/newlogo.png", UriKind.Relative));
                }
                else
                {
                    MemoryStream stream = new MemoryStream(img);
                    bmp.BeginInit();//初始化
                    bmp.StreamSource = stream;//设置源
                    bmp.EndInit();//初始化结束
                }
                double ssum = BuyInfo.Bnum[i] * productInfo.Psale;
                string str = string.Format("{0:N2}", ssum);
                
                order.Add(new OrderGood(bmp, productInfo.Pname, " x " + BuyInfo.Bnum[i].ToString(), "￥" + str));
                sum += ssum;
            }
            SUM.Text = sum.ToString();
            ShopOrderlist.ItemsSource = order;
        }
        //提交订单
        private void Commit_ButtonClick(object sender, RoutedEventArgs e)
        {
            var win1 =(MainWindow)Window.GetWindow(this); //获取父窗口指针
            if (Name.Text == "" || Addr.Text == "" || Mobile.Text == "")
            {
                MessageBox.Show("请前去个人信息页面完善收件信息！");
                return;
            }
            Paying w1 = new Paying(BuyInfo.PidList, BuyInfo.Bnum, SUM.Text, win1);
            //var window = Window.GetWindow(this);
            //window.Close();
            w1.ShowDialog();

        }
    }
}